Conditions | 1 |
Paths | 1 |
Total Lines | 105 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
23 | $.fn.symphonyTimeAgo = function(options) { |
||
24 | var objects = this, |
||
25 | settings = { |
||
26 | items: 'time', |
||
27 | timestamp: 'utc', |
||
28 | max: 0 |
||
29 | }; |
||
30 | |||
31 | $.extend(settings, options); |
||
32 | |||
33 | /*-----------------------------------------------------------------------*/ |
||
34 | |||
35 | Symphony.Language.add({ |
||
36 | 'just now': false, |
||
37 | 'a minute ago': false, |
||
38 | '{$minutes} minutes ago': false, |
||
39 | 'about 1 hour ago': false, |
||
40 | 'about {$hours} hours ago': false |
||
41 | }); |
||
42 | |||
43 | /*------------------------------------------------------------------------- |
||
44 | Functions |
||
45 | -------------------------------------------------------------------------*/ |
||
46 | |||
47 | function parse(item) { |
||
48 | var timestamp = item.data('timestamp'), |
||
49 | datetime; |
||
50 | |||
51 | // Fetch stored timestamp |
||
52 | if($.isNumeric(timestamp)) { |
||
53 | return timestamp; |
||
54 | } |
||
55 | |||
56 | // Parse date |
||
57 | else { |
||
|
|||
58 | datetime = item.attr(settings.timestamp); |
||
59 | |||
60 | // Defined date and time |
||
61 | if(datetime) { |
||
62 | // Datetime will be in seconds since Epoch, JS requires |
||
63 | // millseconds, so multiply by 1000. |
||
64 | timestamp = new Date(datetime * 1000); |
||
65 | } |
||
66 | |||
67 | // Undefined date and time |
||
68 | else { |
||
69 | timestamp = new Date().getTime(); |
||
70 | } |
||
71 | |||
72 | // Store and return timestamp |
||
73 | item.data('timestamp', timestamp); |
||
74 | return timestamp; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | function say(from, to) { |
||
79 | |||
80 | // Calculate time difference |
||
81 | var distance = to - from, |
||
82 | |||
83 | // Convert time to minutes |
||
84 | time = Math.floor(distance / 60000); |
||
85 | |||
86 | // Return relative date based on passed time |
||
87 | if(time < 1) { |
||
88 | return Symphony.Language.get('just now'); |
||
89 | } |
||
90 | if(time < 2) { |
||
91 | return Symphony.Language.get('a minute ago'); |
||
92 | } |
||
93 | if(time < 45) { |
||
94 | return Symphony.Language.get('{$minutes} minutes ago', { |
||
95 | 'minutes': time |
||
96 | }); |
||
97 | } |
||
98 | if(time < 90) { |
||
99 | return Symphony.Language.get('about 1 hour ago'); |
||
100 | } |
||
101 | else if (!settings.max || time < settings.max) { |
||
102 | return Symphony.Language.get('about {$hours} hours ago', { |
||
103 | 'hours': Math.floor(time / 60) |
||
104 | }); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /*------------------------------------------------------------------------- |
||
109 | Initialisation |
||
110 | -------------------------------------------------------------------------*/ |
||
111 | |||
112 | objects.find(settings.items).each(function timeago() { |
||
113 | var item = $(this), |
||
114 | from = parse(item), |
||
115 | to = new Date(), |
||
116 | rel = say(from, to); |
||
117 | |||
118 | // Set relative time |
||
119 | if (rel) { |
||
120 | item.text(rel); |
||
121 | } |
||
122 | }); |
||
123 | |||
124 | /*-----------------------------------------------------------------------*/ |
||
125 | |||
126 | return objects; |
||
127 | }; |
||
128 | |||
130 |